package com.tangye.massageshop.controller;

import com.alibaba.fastjson.JSON;
import com.tangye.massageshop.entity.UserEntity;
import com.tangye.massageshop.service.UserService;
import com.tangye.massageshop.util.Result;
import com.tangye.massageshop.util.ThreadLocalUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.util.List;

@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/queryUserPage")
    public Result<List<UserEntity>> queryUserPage(UserEntity user) {
        log.info("入参：{}",JSON.toJSONString(user));
        UserEntity userSession = ThreadLocalUtil.get();
        user.setCompanyId(userSession.getCompanyId());
        user.setShopId(userSession.getShopId());
        List<UserEntity> userEntities = userService.queryUserListPage(user);
        log.info("反参：{}",JSON.toJSONString(userEntities));
        return Result.success(userEntities);
    }

    @PostMapping("/updatePassword")
    public Result<String> updatePassword(@Validated @Size(min = 5, max = 20, message = "password's length should be 5 ~ 20.") String newPassword) {
        userService.updatePassword(newPassword);
        return Result.ok();
    }

    public static void main(String[] args) {
        int max=200,min=1;
        // 生成10个随机数， 以最后一个为准
        for (int i = 0; i < 10; i++) {
            int ran2 = (int) (Math.random()*(max-min)+min);
            System.out.println(ran2);
        }

    }



}
